<?php
/*
* This module is based on Login & Register Panel (Ajax based) by Marghoob Suleman.
* Original module topic on Prestashop forum: http://www.prestashop.com/forums/topic/57675-module-login-register-panel-ajax-based/.
*/

if (!defined('_PS_VERSION_'))
	exit;
	
class BlockLoginRegister extends Module
{
	public function __construct()
	{
		$this->name = 'blockloginregister';
		$this->tab = 'front_office_features';
		$this->version = '1.2.1';
		$this->author = 'www.kik-off.com';
        $this->need_instance = 0;

		parent::__construct();

        $this->page = basename(__FILE__, '.php');
		$this->displayName = $this->l('User Login & Register');
		$this->description = $this->l('Popup for registration/Login');
	}

	public function install()
	{
		Configuration::updateValue('BL_AJAX_LOGIN', 1);
		
		if (!parent::install() ||
		!Configuration::deleteByName('BL_AJAX_LOGIN') ||
		!$this->registerHook('top') ||
		!$this->registerHook('header'))
			return false;
		return true;
	}

	public function uninstall()
	{
		return parent::uninstall();
	}
	
	public function getContent()
	{
		global $cookie;
		
		$this->_html .= '<h4>'.$this->displayName.'</h4>';
		
		if (Tools::isSubmit('submitLoginRegister'))
		{
		    $bl_ajax_login = Tools::getValue('bl_ajax_login');
		    Configuration::updateValue('BL_AJAX_LOGIN', $bl_ajax_login);
				
			$this->_html .= '<div class="conf">'.$this->l('Block settings updated').'</div>';
		}
		
		$this->_displayForm();
		return $this->_html;
	}
	
	public function _displayForm()
	{
		$this->_html .= '
		<style type="text/css">
		    #translation {}
			#imprint{text-align: right;}
		    #imprint img {float: left;}
		</style>
		<fieldset>
		<legend><img src="../img/admin/nav-user.gif" alt="" title="" />'.$this->l('Settings').'</legend>
			<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
		        <label>'.$this->l('Redirect to Account?').'</label>
				<div class="margin-form">
					<input type="radio" name="bl_ajax_login" id="bl_ajax_login_on" value="1" '.(Tools::getValue('bl_ajax_login', Configuration::get('BL_AJAX_LOGIN')) ? 'checked="checked" ' : '').'/>
					<label class="t" for="bl_ajax_login_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
					<input type="radio" name="bl_ajax_login" id="bl_ajax_login_off" value="0" '.(!Tools::getValue('bl_ajax_login', Configuration::get('BL_AJAX_LOGIN')) ? 'checked="checked" ' : '').'/>
					<label class="t" for="bl_ajax_login_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
					<p>'.$this->l('If disabled, it will remain on the same page where logs on').'.</p>
				</div>
				<div class="margin-form"><input type="submit" name="submitLoginRegister" value="'.$this->l('Save').'" class="button" /></div>
			</form>
		</fieldset>
		';
		
		$this->_html .= '<br/>
		<fieldset>
		<div id="translation">Catalan translation by: <a href="http://www.prestashop.com/forums/user/362294-bdubon/" target="_blank">bdubon</a></div>
		<div class="separation"></div>
		<div id="imprint"><a href="http://www.kik-off.com" target="_blank" title="http://www.kik-off.com"><img src="'.$this->_path.'img/logo_kik_off.gif" alt="" /></a>'.$this->l('Module by').': <a href="http://www.kik-off.com" target="_blank" title="http://www.kik-off.com">www.kik-off.com</a>, <a href="mailto:info@kik-off.com">info@kik-off.com</a></div></fieldset>';

		return $this->_html;
	}

	public function hookTop($params)
	{
		global $smarty;

		$bl_ajax_login = Configuration::get('BL_AJAX_LOGIN');
		
		if (version_compare(_PS_VERSION_,'1.5','<'))
        {
		    if ($params['cookie']->isLogged())
			{
			    return $this->display(__FILE__, $this->name.'-logout.tpl');
			}
			else
			{
		        $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
		        $smarty->assign(array('bl_ajax_login' => $bl_ajax_login));
		        return $this->display(__FILE__, $this->name.'.tpl');
			}
		}
		else
		{
		    if ($this->context->customer->isLogged())
			{
			    return $this->display(__FILE__, $this->name.'-logout.tpl');
			}
			else
			{
		        $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
		        $smarty->assign(array('bl_ajax_login' => $bl_ajax_login));
		        return $this->display(__FILE__, $this->name.'.tpl');
			}
		}
	}

	public function hookRightColumn($params)
	{
		return $this->hookTop($params);
	}

	public function hookLeftColumn($params)
	{
		return $this->hookTop($params);
	}

	public function hookHeader($params)
	{
	    global $smarty, $cookie;
        
		if (version_compare(_PS_VERSION_,'1.5','<'))
        {
		    Tools::addCSS($this->_path.'css/loginregister.css', 'all');
		    Tools::addCSS(_PS_CSS_DIR_.'jquery.fancybox-1.3.4.css', 'all');
		    Tools::addJS($this->_path.'js/blockloginregister.min.js');
		    Tools::addJS(_PS_JS_DIR_.'jquery/jquery.fancybox-1.3.4.js');
		}
		else
		{
		    $this->context->controller->addCSS(($this->_path).'css/loginregister.css', 'all');
			$this->context->controller->addJS(($this->_path).'js/blockloginregister.min.js');
			$this->context->controller->addCSS(_PS_JS_DIR_.'jquery/plugins/fancybox/jquery.fancybox.css', 'all');
			$this->context->controller->addJS(_PS_JS_DIR_.'jquery/plugins/fancybox/jquery.fancybox.js');
		}

	}
}

?>
